package com.minggo.charmword.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.baidu.mobstat.StatService;
import com.minggo.charmword.R;
import com.minggo.charmword.logic.UserLoginUtil;
import com.minggo.charmword.model.User;
import com.minggo.charmword.service.CharmWordApplication;
import com.minggo.charmword.util.UserUtil;

/**
 * 登录页面
 * 
 * @author minggo
 * @time 2014-6-28 S下午10:00:11
 */
public class LoginActivity extends Activity implements OnClickListener {

	private View backBt;
	private EditText userNameEd;
	private EditText passwordEd;
	private Button loginBt;
	private Button registerBt;
	private ProgressBar loadingPb;
	public static final int LOGIN_SUCCESS = 10001;
	public static final int LOGIN_FAILED = 1;
	private UserLoginUtil userLoginUtil;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		initUI();

		CharmWordApplication.allActivities.add(this);
	}

	/**
	 * 初始化UI
	 */
	private void initUI() {
		backBt = findViewById(R.id.lo_login_back);
		userNameEd = (EditText) findViewById(R.id.ed_username);
		passwordEd = (EditText) findViewById(R.id.ed_password);
		loginBt = (Button) findViewById(R.id.bt_login);
		registerBt = (Button) findViewById(R.id.bt_register);
		loadingPb = (ProgressBar) findViewById(R.id.pb_loading);

		backBt.setOnClickListener(this);
		loginBt.setOnClickListener(this);
		registerBt.setOnClickListener(this);

		loadingPb.setVisibility(View.GONE);
	}

	/**
	 * 登陆操作
	 */
	private void login() {
		String email = userNameEd.getText().toString();
		String pass = passwordEd.getText().toString();
		if (!email.equals("") && !pass.equals("")) {
			if (email.contains("@")) {
				User user = new User();
				user.email = email;
				user.password = pass;
				loadingPb.setVisibility(View.VISIBLE);
				userLoginUtil = new UserLoginUtil(this, handler, user);
				userLoginUtil.execute();
				loginBt.setEnabled(false);
			} else {
				Toast.makeText(this, R.string.input_email_right, Toast.LENGTH_SHORT).show();
			}
		} else {
			Toast.makeText(this, R.string.input_null, Toast.LENGTH_SHORT).show();
		}
	}

	private Handler handler = new Handler(new Callback() {

		@Override
		public boolean handleMessage(Message msg) {

			switch (msg.what) {
			case UserLoginUtil.USER_LOGIN:
				loadingPb.setVisibility(View.GONE);
				loginBt.setEnabled(true);
				if (msg.obj != null) {
					User user = (User) msg.obj;
					UserUtil.saveUser(LoginActivity.this, user);
					
					Toast.makeText(LoginActivity.this, "登录成功", Toast.LENGTH_LONG).show();
					LoginActivity.this.setResult(LOGIN_SUCCESS);
					finish();
				} else {
					Toast.makeText(LoginActivity.this, "登录失败", Toast.LENGTH_LONG).show();
				}
				break;

			default:
				break;
			}

			return false;
		}
	});

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.lo_login_back:
			onBackPressed();
			break;
		case R.id.bt_login:
			login();
			break;
		case R.id.bt_register:
			startActivityForResult(new Intent(this, RegisterActivity.class), 10001);
			break;

		default:
			break;
		}

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (resultCode) {
		case RegisterActivity.REGISTER_USER:
			LoginActivity.this.setResult(LOGIN_SUCCESS);
			finish();
			break;

		default:
			break;
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		StatService.onResume(this);
	}

	@Override
	protected void onPause() {
		super.onPause();
		StatService.onPause(this);
	}

	@Override
	public void onBackPressed() {
		if (userLoginUtil != null && !userLoginUtil.isCancelled()) {
			userLoginUtil.cancel(true);
		}
		super.onBackPressed();
		finish();
	}
}
